import lodash from 'lodash'
import Vue from 'vue'
export function proxy(obj) {
    return new Proxy(obj, {
        get(target, p, receiver) {
            return target[p]
        },
        set(target, p, value, receiver) {
            target[p] = value;
            return true
        }
    });
}
export function proxyObject(source,{get=null,set=null}){
    return new Proxy(source, {
        get(target, p, receiver) {
            if(!!get){
                get(target, p);
            }
            return target[p]
        },
        set(target, p, value, receiver) {
            target[p] = value;
            if(!!set){
                set(target, p, value);
            }
            return true
        }
    });
}

/**
 * 深度复制
 */
// 定义一个深拷贝函数  接收目标target参数
export function flatData(source,target,key,reactive) {
    reactive=reactive||false;
    if (typeof source === 'object') {
        if (Array.isArray(source)) {
            for (let i in source) {
                let k=key===''?'['+i+']':(key+'['+i+']');
                target[k]=source[i];
                if(reactive) {
                    Object.defineProperty(target, k, {
                        configurable: true,
                        enumerable: true,
                        get: function () {
                            return source[i];
                        },
                        set: function (val) {
                            source[i] = val;
                            return true;
                        }
                    });
                }
                flatData(source[i],target,k,reactive);
            }
        } else if(source===null) {
            target[key] = null;
        } else if(source.constructor===RegExp){
            target[key] = source;
        }else {
            for (let i in source) {
                let k=key===''?i:key+'.'+i;
                target[k] = source[i];
                if(reactive) {
                    Object.defineProperty(target, k, {
                        configurable: true,
                        enumerable: true,
                        get: function () {
                            return source[i];
                        },
                        set: function (val) {
                            source[i] = val;
                            return true;
                        }
                    });
                }
                flatData(source[i],target,k,reactive);
            }
        }
    } else {
        target = source;
    }
    return target;
}
export function deepProxy(Obj) {
    // 深度遍历
    if (typeof Obj === 'object') {
        const status = Array.isArray(Obj);
        if (status) {
            Obj.forEach((v,i) => {
                if (typeof v === 'object') {
                    Obj[i] = deepProxy(v)
                }
            })
        } else  {

            Object.keys(Obj).forEach(v => {
                if (typeof Obj[v] === 'object') {
                    Obj[v] = deepProxy(Obj[v])
                }
            });
        }
        const pxy =proxy(Obj);
        return pxy;
    }
    return new TypeError("Argument must be object or array");
}
const proxyTool={proxy,deepProxy,flatData,proxyObject}
export default proxyTool;